<?php echo $this->Html->script('//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false); ?>
<?php echo $this->Html->script('//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js', false); ?>
<?php echo $this->Html->script('http://blueimp.github.io/JavaScript-Load-Image/js/load-image.min.js', false); ?>
<?php echo $this->Html->script('http://blueimp.github.io/JavaScript-Canvas-to-Blob/js/canvas-to-blob.min.js', false); ?>
<?php echo $this->Html->script('jquery/jQuery-File-Upload-8.8.2/js/jquery.iframe-transport.js', false); ?>
<?php echo $this->Html->script('jquery/jQuery-File-Upload-8.8.2/js/jquery.fileupload.js', false); ?>
<?php echo $this->Html->script('jquery/jQuery-File-Upload-8.8.2/js/jquery.fileupload-process.js', false); ?>
<?php echo $this->Html->script('jquery/jQuery-File-Upload-8.8.2/js/jquery.fileupload-image.js', false); ?>
<?php echo $this->Html->script('jquery/jQuery-File-Upload-8.8.2/js/jquery.fileupload-validate.js', false); ?>
<?php echo $this->Html->script('jquery/jQuery-Tagit/js/tagit.js', false); ?>
<?php echo $this->Html->css('//ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css');?>
<?php echo $this->Html->css('photoupload.css');?>
<?php echo $this->Html->css('tagit-simple-grey.css');?>
 
<script>
var myData;
$(document).ready(function() {
	$('#PostTags').tagit({select: true});
	$('#PostPhotos').fileupload({
        //url: "server/php/",
        dataType: 'json',
        autoUpload: false,
        singleFileUploads: false,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 5000000, // 5 MB
        // Enable image resizing, except for Android and Opera,
        // which actually support image resizing, but fail to
        // send Blob objects via XHR requests:
        disableImageResize: true,
        previewMaxWidth: 100,
        previewMaxHeight: 100,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {
        data.context = $('<div/>').appendTo('#files');
        $.each(data.files, function (index, file) {
            var node = $('<p/>')
                    .append($('<span/>').text(file.name));
            if (!index) {
                node
                    .append('<br>')
                    //.append(uploadButton.clone(true).data(data));
            }
            node.appendTo(data.context);
        });
    }).on('fileuploadprocessalways', function (e, data) {
        var index = data.index,
            file = data.files[index],
            node = $(data.context.children()[index]);
        if (file.preview) {
            node
                .prepend('<br>')
                .prepend(file.preview);
        }
        if (file.error) {
            node
                .append('<br>')
                .append(file.error);
        }
        if (index + 1 === data.files.length) {
            data.context.find('button')
                .text('Upload')
                .prop('disabled', !!data.files.error);
        }
        myData = data;
        console.log(myData);
    }).on('fileuploadprogressall', function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .bar').css(
            'width',
            progress + '%'
        );
    }).on('fileuploaddone', function (e, data) {
//        $.each(data.result.files, function (index, file) {
//            var link = $('<a>')
//                .attr('target', '_blank')
//                .prop('href', file.url);
//            $(data.context.children()[index])
//                .wrap(link);
//        });
    }).on('fileuploadfail', function (e, data) {
        $.each(data.result.files, function (index, file) {
            var error = $('<span/>').text(file.error);
            $(data.context.children()[index])
                .append('<br>')
                .append(error);
        });
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
});

function submit() {
	myData.form = $('#PostAddForm');
	myData.submit(); 
}

//$(document).ready(function() {
	
//});
</script>

<h1>Add Post</h1>
<?php
echo $this->Form->create('Post', array('type' => 'file', 'onsubmit' => 'return false;'));
echo $this->Form->input('title');
echo $this->Form->input('content', array('rows' => '3')); ?>
<div class="input">
	<label for="PostTags">Tags</label>
	<ul id="PostTags" name="data[Post][tags][]"></ul>
</div>
<?php echo $this->Form->input('photos.', array('type' => 'file', 'multiple')); ?>
<div id="progress">
        <div class="bar" style="width: 0%;"></div>
</div>
<div id="files" class="files"></div>
<?php echo $this->Form->end(); ?>
<button type="button" onclick="submit();">Save post</button>
<!-- <script>

$(function () {
    $('#PostPhotos').fileupload({
        dataType: 'json',
        singleFileUploads:false,
        add: function (e, data) {
            data.context = $('<button/>').text('Upload')
                .appendTo(document.body)
                .click(function () {
                    data.context = $('<p/>').text('Uploading...').replaceAll($(this));
                    data.submit();
                });
        },
        done: function (e, data) {
            data.context.text('Upload finished.');
        }
    });
});

</script> -->